<?php
class IndexController extends Controller{

	public function index(){
		$this->login();
	}
	/*
	 *登录 
	 */
	public function login(){
		if($_POST){
			$username = $_POST['username'];
			$passwd = md5($_POST['passwd']);
			$user_value = M('hd_admin')->where("username = '{$username}'")->find();
			if(!empty($user_value)){
				if($user_value['username'] == $username && $user_value['passwd'] == $passwd ){
					$_SESSION['username'] = $username;
					setcookie(session_name(),session_id(),0,'/');	
					//记录登陆时间 登陆ip
					
					if($_SESSION['code'] != strtoupper($_POST['code'])){
						$this->error('验证码不正确');						
					}
					$time = time();
					M('hd_admin')->exec("UPDATE hd_admin SET logintime='{$time}',loginip='{$_SERVER['SERVER_ADDR']}' WHERE username='{$username}'");			
					$this->success('登录成功','./admin.php?c=index&a=welcome');
				}else{
					$this->error('用户名或密码错误输入错误');
				}
			}else{
				$this->error('用户名不存在');
			}
			
		}
		$this->display();
	}


	/*
	 * 判断是否合法路径访问  是否登录 
	 */
	public function Islogin(){
			if(empty($_SESSION['username'])){
				$this->success('不合法路径,请登录后执行操作',__ROOT__ .'/admin.php');
			};
	}
/*
 * 管理首页
 */	
	public function welcome(){
		$this->Islogin();
		$this->display();
	}
	/*
	 * 登出
	 */
	 public function LoginOut(){
	 	$this->Islogin();
	 	session_unset();
		session_destroy();
		$this->success('注销成功','./index.php');
	 }
	/*
	 * 显示验证码
	 */
	public function Code(){
		$code =new Code();
		$code->ImgCodeShow();
		
	}

	/*
	 * 所有问题
	 */
	public function askList(){
		$askList = M('hd_ask')->query("SELECT * FROM hd_ask");
		$this->assign('askList', $askList);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 待解决问题
	 */
	public function unokList(){
		$unokList = M('hd_ask')->query("SELECT * FROM hd_ask WHERE solve=0");
		$this->assign('unokList', $unokList);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 已解决问题
	 */
	public function okList(){
		$okList = M('hd_ask')->query("SELECT * FROM hd_ask WHERE solve=1");
		$this->assign('okList', $okList);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 无回答问题
	 */
	public function noAnswerkList(){
		$noAnswerkList = M('hd_ask')->query("SELECT * FROM hd_ask WHERE answer=0");
		$this->assign('noAnswerkList', $noAnswerkList);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 所有回答
	 */
	public function allAnswer(){
		$allAnswer = M('hd_answer')->query("SELECT * FROM hd_answer");
		$this->assign('allAnswer', $allAnswer);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 未采纳回答
	 */
	public function noAnswer(){
		$noAnswer = M('hd_answer')->query("SELECT * FROM hd_answer WHERE accept =0");
		$this->assign('noAnswer', $noAnswer);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 已采纳回答
	 */
	public function AnswerList(){
		$AnswerList = M('hd_answer')->query("SELECT * FROM hd_answer WHERE accept =1");
		$this->assign('AnswerList', $AnswerList);
		$this->Islogin();
		$this->display();
	}
//	/*
//	 * 问题分类列表
//	 */
//	public function AnswerCid(){
//		$this->Islogin();
//		$AnswerCid = M("hd_category")->query("SELECT * FROM hd_category");
//	   	$this->assign('AnswerCid', $AnswerCid);
//	   	$this->display();
//	}
	/*
	 * 问题分类列表
	 */
	public function AnswerCid(){
		$this->Islogin();
		$AnswerCid = M("hd_category")->query("SELECT * FROM hd_category WHERE PID=0");
	   	$this->assign('AnswerCid', $AnswerCid);
	  $this->display();
	}
	/*
	 * 获取分类
	 */
	 public function getcid(){
		if(IS_POST){
		$pid = (int)$_POST['pid'];
		$son = M('hd_category')->query("SELECT * FROM hd_category where pid={$pid}");
		echo json_encode($son);
		}
	 }
	/*
	 * 添加顶级分类
	 */
	public function AnswerAddCid(){
		$this->Islogin();
		if(IS_POST){
			$title = $_POST['title'];
			$user_value = M('hd_category')->where("title = '{$title}'")->find();
			if(empty($user_value)){
				M('hd_user')->exec("INSERT INTO hd_category SET title='{$title}', pid=0 ");
				$this->error('栏目添加成功');
			}else{
				$this->error('栏目名已存在');
			}	
		}
		$this->display();
	}
	/*
	 * 经验级别规则
	 */
	public function userLv(){
		$this->Islogin();
		$this->display();
	}
	/*
	 * 经验级别规则
	 */
	public function userGold(){
		$this->Islogin();
		$data = include './index/Config/Config.php';
		$this->assign('data', $data);
		if(IS_POST){
			if(empty($_POST['answer'])){
				$_POST['answer'] = C('answer');
			}
			if(empty($_POST['accept'])){
				$_POST['accept'] = C('accept');
			}
			if(empty($_POST['answerdel'])){
				$_POST['answerdel'] = C('answerdel');
			}
			if(empty($_POST['askdel'])){
				$_POST['askdel'] = C('askdel');
			}
			
			$data['answer'] = $_POST['answer'];
			$data['accept'] = $_POST['accept'];
			$data['answerdel'] = $_POST['answerdel'];
			$data['askdel'] = $_POST['askdel'];
			data_to_file($data,'./index/Config/Config.php');
			$this->success('修改成功', './admin.php?c=index&a=userGold');
		}
		$this->Islogin();
		$this->display();
	}
	/*
	 * 系统信息
	 */
	public function webinfo(){
		$username = $_SESSION['username'];
		$admininfo = M('hd_admin')->query("SELECT * FROM hd_admin WHERE username = '{$username}' ");
		$admininfo = current($admininfo);
		$this->assign('admininfo', $admininfo);
			$webinfo = array(
			'webserver' => $_SERVER['SERVER_SOFTWARE'],
			'phpversion' => PHP_VERSION,
			'localhost'  => $_SERVER['HTTP_HOST'],
		   	'mysql'  =>$_SERVER['SERVER_SOFTWARE'],			
		);
		$this->assign('webinfo',$webinfo);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 修改密码
	 */
	public function editPassword(){
		if(IS_POST){
		$username = $_POST['username'];
		$password = md5($_POST['passwd']);
		$uservalue = M('hd_answer')->query("SELECT * FROM hd_user WHERE username='{$username}' ");
		if(empty($uservalue)){
			$this->error('用户名不存在');
		}
		M('hd_answer')->exec("UPDATE hd_user set passwd='{$password}' WHERE username='{$username}' ");
		$this->success("添加成功", './admin.php?c=index&a=webnfo');
		}
		$this->Islogin();
		$this->Islogin();
		$this->display();
	}	
	/*
	 * 网站配置
	 */
	public function webConfig(){
		$data = include './index/Config/Config.php';
		$this->assign('data', $data);
		if(IS_POST){
			if(!isset($_POST['RES_ON'])){
				$_POST['RES_ON'] = 1;
			}
			if(!isset($_POST['WEB_ON'])){
				$_POST['WEB_ON'] = 1;
			}
			if(empty($_POST['WEBNAME'])){
				$this->success('网站名称没有填写！', './admin.php?c=index&a=webConfig');
			}
			if(empty($_POST['KEYWORDS'])){
				$this->success('网站关键词没有填写！', './admin.php?c=index&a=webConfig');
			}
			if(empty($_POST['DISCRIPTION'])){
				$this->success('网站描述没有填写！', './admin.php?c=index&a=webConfig');
			}
			
			$data['WEBNAME'] = $_POST['WEBNAME'];
			$data['KEYWORDS'] = $_POST['KEYWORDS'];
			$data['DISCRIPTION'] = $_POST['DISCRIPTION'];
			$data['COPY'] = $_POST['COPY'];
			$data['RECORD'] = $_POST['RECORD'];
			$data['RES_ON'] = $_POST['RES_ON'];
			$data['WEB_ON'] = $_POST['WEB_ON'];
			data_to_file($data,'./index/Config/Config.php');
			$this->success('修改成功', './admin.php?c=index&a=webConfig');
		}
		$this->Islogin();
		$this->display();
	}	
	/*
	 * 用户列表
	 */
	public function userList(){
		$userInfo = M('hd_user')->query("SELECT * FROM hd_user");
		$this->assign('userInfo', $userInfo);

		$this->Islogin();
		$this->display();
	}
	/*
	 * 锁定用户
	 */
	 public function lockUser(){
	 	$this->Islogin();
	 	$uid = $_GET['uid'];
		M('hd_admin')->exec("UPDATE hd_user SET lockuser='1' WHERE uid='{$uid}' ");
		$this->error('锁定用户成功');
	 }
	/*
	 * 解除锁定
	 */
	 public function openUser(){
	 	$this->Islogin();
	 	$uid = $_GET['uid'];
		M('hd_admin')->exec("UPDATE hd_user SET lockuser='0' WHERE uid='{$uid}' ");
		$this->error('用户锁定已解除');
	 }
	/*
	 * 删除用户
	 */
	 public function delUser(){
	 	$this->Islogin();
	 	$uid = $_GET['uid'];
		M('hd_admin')->exec("DELETE FROM hd_user WHERE uid = '{$uid}' ");
		$this->error('删除成功');
	 }
	 public function administor(){
	 	$this->Islogin();
	 	$aid = $_GET['aid'];
		M('hd_admin')->exec("DELETE FROM hd_admin WHERE aid = '{$aid}' ");
		$this->error('删除成功');
	 }
	 /*
	 * 删除问题
	 */
	 public function delask(){
	 	$this->Islogin();
	 	$asid = $_GET['asid'];
		M('hd_admin')->exec("DELETE FROM hd_ask WHERE asid = '{$asid}' ");
		$this->error('删除成功');
	 }
	 /*
	 * 删除回答
	 */
	 public function delAnswer(){
	 	$this->Islogin();
	 	$anid = $_GET['anid'];
		M('hd_admin')->exec("DELETE FROM hd_answer WHERE anid = '{$anid}' ");
		$this->error('删除成功');
	 }
	/*
	 * 后台用户列表
	*/
	public function managerList(){
		$managerList = M('hd_user')->query("SELECT * FROM hd_admin");
		$this->assign('managerList', $managerList);
		$this->Islogin();
		$this->display();
	}
	/*
	 * 添加用户
	 */
	public function addUser(){
		if(IS_POST){
		$username = $_POST['username'];
		$password = md5($_POST['passwdF']);
		M('hd_answer')->exec("INSERT INTO hd_admin set username='{$username}',passwd='{$password}'");
		$this->success("添加成功", './admin.php?c=index&a=webnfo');
		}
		$this->Islogin();
		$this->display();
	}
	
}
	
	
	
	
?>